package com.ltu.service.tencent;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.ltu.common.Constants;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.iotexplorer.v20190423.IotexplorerClient;
import com.tencentcloudapi.iotexplorer.v20190423.models.*;

import lombok.extern.slf4j.Slf4j;
/**
 * 腾讯云 物联网开发平台
 * @author Administrator
 */
@Slf4j
@Component
public class TencentIotExplorerUtil { 
	
	private final static String  ENDPOINT="iotexplorer.tencentcloudapi.com";
	private final static String  REGION="ap-guangzhou";
	private static  Credential cred;
	private static String  SecretId="AKID97pLbaIjtmSrzqPimZ5LK2TdW8TWJp42" ;
    private static String  SecretKey="jBMFsDH4apnVv4wXrwlJaaKL5YkfIern";

	public TencentIotExplorerUtil() {
	
	}
	//数据通过腾讯物联网下发到设备
	public static void sendDeviceData(String ProductId,String DeviceName,String jsonData) {
		        try{
		            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
		            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
		        	if(ObjectUtils.isEmpty( cred))
//		        		cred = new Credential(Constants.TenCentSecretId,  Constants.TenCentSecretKey);
		        		cred = new Credential(SecretId,  SecretKey);
		            // 实例化一个http选项，可选的，没有特殊需求可以跳过
		            HttpProfile httpProfile = new HttpProfile();
		            httpProfile.setEndpoint(ENDPOINT);
		            // 实例化一个client选项，可选的，没有特殊需求可以跳过
		            ClientProfile clientProfile = new ClientProfile();
		            clientProfile.setHttpProfile(httpProfile);
		            // 实例化要请求产品的client对象,clientProfile是可选的
		            IotexplorerClient client = new IotexplorerClient(cred, REGION , clientProfile);
		            // 实例化一个请求对象,每个接口都会对应一个request对象
		            ControlDeviceDataRequest req = new ControlDeviceDataRequest();
		            req.setProductId(ProductId);
		            req.setDeviceName(DeviceName);
		            req.setData(jsonData);
		            // 返回的resp是一个ControlDeviceDataResponse的实例，与请求对象对应
		            ControlDeviceDataResponse resp = client.ControlDeviceData(req);
		            // 输出json格式的字符串回包
		            String resultStr= ControlDeviceDataResponse.toJsonString(resp);
		            log.info(resultStr);
		            JSONObject  result=JSONObject.parseObject(resultStr);
		            JSONObject  childresult =  result.getJSONObject("Result");
		            if(childresult.getIntValue("pushResult")!=0) 
		            	log.error("\n 下发数据失败：{}",childresult.getString("pushResult"));
		          
		        } catch (TencentCloudSDKException e) {
		            System.out.println(e.toString());
		        }
		    }
	

	
	public static void main(String[] args) {
		//{"Data":"","Result":"{\"Sent\":1,\"pushResult\":0}","RequestId":"c02eee68-7e15-48ab-bb36-6065a7789888"}
        String  jsonData="{ \"modelOpttion\": 4,       \"status\": 1,       \"runSpeed\": 4,       \"manualRunTime\": 5,       \"runOutTime\": 33,       \"firstRunTime\": 33,       \"forOutTime\": 31,       \"forRunTime\": 10}";
		sendDeviceData("LJUX55VQ05","CLJ",jsonData);
	}
	
}
